﻿@*
    列表视图 - 表格模式
*@
@using NewLife;
@using NewLife.Web;
@using XCode;
@using XCode.Configuration;
@{
    var fact = ViewBag.Factory as IEntityFactory;
    var page = ViewBag.Page as Pager;
    var fields = ViewBag.Fields as FieldCollection;
    var ukey = fact.Unique;
    var set = ViewBag.PageSetting as PageSetting ?? PageSetting.Global;
}

<div class="card card-flush">
    <div class="card-body">
        <div class="table-responsive dataTables_wrapper dt-bootstrap4">
            <table class="table table-hover table-row-bordered table-row-dashed align-middle gy-4">
                <thead>
                    <tr class="fs-4 text-muted">
                        @if (set.EnableSelect && ukey != null)
                        {
                            <th class="text-center" style="width:10px;">
                                <div class="form-check form-check-sm form-check-custom form-check-solid">
                                    <input class="form-check-input" type="checkbox" id="chkAll" data-bs-toggle="tooltip" title="全选" />
                                </div>
                            </th>
                        }
                        @foreach (var item in fields)
                        {
                            var sortUrl = !item.MapField.IsNullOrEmpty() ? page.GetSortUrl(item.MapField) : page.GetSortUrl(item.Name);
                            var df = item as ListField;
                            var name = df?.Header ?? item.DisplayName;
                            var title = df?.HeaderTitle ?? item.Description;

                            <!-- 跳过外部指向外部链接的列表字段 -->
                            if (df != null && (!df.DataAction.IsNullOrEmpty() || !df.Url.IsNullOrEmpty()))
                                continue;

                            if (item.PrimaryKey || item.Type == typeof(Boolean))
                            {
                                <th class="text-center" data-bs-toggle="tooltip" title="@title">@name</th>
                            }
                            else if (item.Type == typeof(DateTime) && item.Services.Count == 0)
                            {
                                var width = item.ItemType == "Date" ? 80 : 134;
                                <th style="min-width:@(width)px;" data-bs-toggle="tooltip" title="@title"><a href="@Html.Raw(sortUrl)">@name</a></th>
                            }
                            else if (item.Type == typeof(Int32) || item.Type == typeof(Int64) || item.Type == typeof(Double) || item.Type == typeof(Decimal)
                            || item.Type == typeof(Single) || item.Type == typeof(UInt32) || item.Type == typeof(UInt64))
                            {
                                <th class="text-end" data-bs-toggle="tooltip" title="@item.Description"><a href="@Html.Raw(sortUrl)">@item.DisplayName</a></th>
                            }
                            else if (item.Type == null)
                            {
                                <th class="text-center" data-bs-toggle="tooltip" title="@title">@name</th>
                            }
                            else
                            {
                                <th data-bs-toggle="tooltip" title="@title"><a href="@Html.Raw(sortUrl)">@name</a></th>
                            }
                        }
                        @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
                        {
                            <th>操作</th>
                        }
                    </tr>
                </thead>
                <tbody>
                    @foreach (var entity in Model)
                    {
                        <tr class="@(fields.GetRowClass?.Invoke(entity))">
                            @if (set.EnableSelect && ukey != null)
                            {
                                <td class="text-center">
                                    <div class="form-check form-check-sm form-check-custom form-check-solid">
                                        <input class="form-check-input" type="checkbox" name="keys" value="@entity[ukey.Name]" />
                                    </div>
                                </td>
                            }
                            @foreach (var item in fields)
                            {
                                var df = item as ListField;
                                <!-- 跳过外部指向外部链接的列表字段 -->
                                if (df != null && (!df.DataAction.IsNullOrEmpty() || !df.Url.IsNullOrEmpty()))
                                    continue;

                                if (df != null && df.DataVisible != null && !df.DataVisible(entity))
                                {
                                    <td></td>
                                }
                                else
                                {
                                    @await Html.PartialAsync("_List_Data_Item", new ValueTuple<IEntity, DataField>(entity, item))
                                }
                            }
                            @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
                            {
                                <td>
                                    @await Html.PartialAsync("_List_Data_Action", (Object)entity)
                                </td>
                            }
                        </tr>
                    }
                    @if (page.State is IEntity entity2)
                    {
                        @await Html.PartialAsync("_List_Data_Stat", entity2)
                    }
                </tbody>
            </table>
        </div>
    </div>
</div>
